package prototype

import (
	"fmt"
	"strings"
)

// BaseProductCard 基础商品卡片
type BaseProductCard struct {
	productName string   // 商品名称
	price       float64  // 商品价格
	imageURL    string   // 商品图片URL
	tags        []string // 个性化标签
	reason      string   // 个性化推荐理由
	promotion   string   // 个性化促销信息
}

func NewBaseProductCard(productName string, price float64, imageURL string) *BaseProductCard {
	return &BaseProductCard{
		productName: productName,
		price:       price,
		imageURL:    imageURL,
	}
}

// Clone 复制基础卡片
func (b *BaseProductCard) Clone() ProductCard {
	clone := &BaseProductCard{
		productName: b.productName,
		price:       b.price,
		imageURL:    b.imageURL,
		tags:        b.tags,
		reason:      b.reason,
		promotion:   b.promotion,
	}
	// 深拷贝切片
	clone.tags = make([]string, len(b.tags))
	copy(clone.tags, b.tags)
	return clone
}

// SetTags 设置个性化标签
func (b *BaseProductCard) SetTags(tags []string) {
	b.tags = tags
}

// SetReason 设置个性化推荐理由
func (b *BaseProductCard) SetReason(reason string) {
	b.reason = reason
}

// SetPromotion 设置个性化促销信息
func (b *BaseProductCard) SetPromotion(promotion string) {
	b.promotion = promotion
}

// Render 渲染基础卡片内容
func (b *BaseProductCard) Render() string {
	sb := strings.Builder{}
	sb.WriteString("<div class='product-card'>\n")
	sb.WriteString(fmt.Sprintf("  <img src='%s' alt='%s'>\n", b.imageURL, b.productName))
	sb.WriteString(fmt.Sprintf("  <h3>%s</h3>\n", b.productName))
	sb.WriteString(fmt.Sprintf("  <p class='price'>¥%.2f</p>\n", b.price))

	if len(b.tags) > 0 {
		sb.WriteString("  <div class='tags'>\n")
		for _, tag := range b.tags {
			sb.WriteString(fmt.Sprintf("    <span>%s</span>", tag))
		}
		sb.WriteString("\n  </div>\n")
	}

	if b.reason != "" {
		sb.WriteString(fmt.Sprintf("  <p class='reason'>%s</p>\n", b.reason))
	}

	if b.promotion != "" {
		sb.WriteString(fmt.Sprintf("  <div class='promo'>%s</div>\n", b.promotion))
	}

	sb.WriteString("</div>")
	return sb.String()
}
